<template>
  <el-form label-width="80px" :model="formData" :rules="rules" ref="form">
    <div id="way-picker">
      <way-picker
        v-model:radio="formData.radio"
        :name1="name1"
        :name2="name2"
      ></way-picker>
      <decoration></decoration>
    </div>
    <el-row>
      <el-col :span="11">
        <city-picker
          prop="departureCity"
          type="出发城市"
          :cityList="cityListArr"
          placeholder="请选择出发城市"
          v-model:currCityName="formData.departureCity"
        ></city-picker>
      </el-col>
      <el-col :span="2">
        <el-button
          class="form-btn-switch"
          @click="exchangeCity"
          circle
        ></el-button>
      </el-col>
      <el-col :span="11">
        <city-picker
          prop="arrivalCity"
          type="到达城市"
          :cityList="cityListArr"
          placeholder="请选择到达城市"
          v-model:currCityName="formData.arrivalCity"
        ></city-picker>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="11">
        <date-picker
          type="出发时间"
          prop="departureDate"
          v-model:dateTime="formData.departureDate"
        ></date-picker>
      </el-col>
      <el-col :span="2">
        <el-button class="form-btn-plane" circle></el-button>
      </el-col>
      <el-col :span="11">
        <date-picker
          type="返程时间"
          :disabled="formData.radio"
          prop="returnDate"
          v-model:dateTime="formData.returnDate"
        ></date-picker>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="11">
        <passenger-picker
          prop="passengerInfo"
          v-model:passengerInfo="formData.passengerInfo"
        ></passenger-picker>
      </el-col>
      <el-col :span="11">
        <el-form-item>
          <el-button class="submit" @click="onSubmit">搜&nbsp;索</el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script lang="ts">
import { defineComponent, ref, reactive, provide, Ref, onMounted } from 'vue';
import CityPicker from '../../components/CityPicker.vue';
import DatePicker from '../../components/DatePicker.vue';
import WayPicker from '../../components/Picker.vue';
import PassengerPicker from '../../components/PassengerPicker.vue';
import Decoration from '@/components/Common/Decoration.vue';
import router from '@/router/index';
import { useStore } from 'vuex';
// interface formData {
//   departureCity: string;
//   arrivalCity: string;
//   departureDate: Date;
//   returnDate: Date;
//   passengerInfo: string;
//   pAmount: Ref<number[]>;
// }
export default defineComponent({
  name: 'Ticket',
  components: {
    CityPicker,
    DatePicker,
    WayPicker,
    PassengerPicker,
    Decoration,
  },
  setup(props, context) {
    // const formData = ref({
    //   departureCity: '',
    //   arrivalCity: '',
    //   departureDate: null,
    //   returnDate: null,
    //   passengerInfo: '',
    //   pAmount: ref<number[]>([0, 0, 0]),
    //   // pAmount: ,
    // });
    const formData = ref(useStore().state.formData);
    provide('num', formData.value.pAmount);
    const name1 = '单程';
    const name2 = '往返';
    // const formData.radio = ref(true);
    const cityListArr = reactive({
      hot: [{ id: 111111, name: '北京市' }],
      A: [{ id: 111111, name: '安阳市' }],
      B: [{ id: 111111, name: '北京市' }],
      H: [{ id: 111111, name: '杭州市' }],
    });
    const exchangeCity = () => {
      [formData.value.departureCity, formData.value.arrivalCity] = [
        formData.value.arrivalCity,
        formData.value.departureCity,
      ];
    };
    //自定义验证器
    const validateCity = (rule: any, value: string, callback: Function) => {
      if (value === '') {
        callback(new Error('到达城市不可为空'));
      } else {
        if (formData.value.arrivalCity === formData.value.departureCity) {
          callback(new Error('出发到达机场不能在同一个城市里!'));
        } else {
          callback();
        }
      }
    };
    const validatePass = (rule: any, value: string, callback: Function) => {
      if (value === '') {
        callback(new Error('乘机人数不可为空'));
      } else {
        if (
          formData.value.pAmount[0] * 3 <
          formData.value.pAmount[1] + formData.value.pAmount[2]
        ) {
          callback(new Error('一位成人最多携带3名非成人!'));
        } else {
          callback();
        }
      }
    };
    const validateDepartureDate = (
      rule: any,
      value: Date,
      callback: Function
    ) => {
      if (value === null) {
        callback(new Error('出发时间不可为空!'));
      } else {
        const today = new Date();
        //让当天时间加1
        if (
          today >
          new Date(
            (formData.value.departureDate as any).getTime() + 3600 * 1000 * 24
          )
        ) {
          callback(new Error('出发时间不可早于现在时间!'));
        } else {
          callback();
        }
      }
    };
    const validateReturnDate = (rule: any, value: Date, callback: Function) => {
      if (formData.value.radio === true) {
        callback();
      }
      if (value === null) {
        callback(new Error('返程时间不可为空!'));
      } else {
        if (
          (formData.value.departureDate as any) >
          (formData.value.returnDate as any)
        ) {
          callback(new Error('返程时间不可早于出发时间!'));
        } else {
          callback();
        }
      }
    };
    //表单数据验证
    const form = ref(null);
    const rules = {
      departureCity: [
        { required: true, message: '出发城市不可为空!', trigger: 'change' },
      ],
      arrivalCity: [{ validator: validateCity, trigger: 'change' }],
      passengerInfo: [
        // { required: true, message: '乘机人数不可为空', trigger: 'change' },
        { validator: validatePass, trigger: 'change' },
      ],
      // pAmount: [{ required: true, message: '乘机人错误', trigger: 'change' }],
      departureDate: [{ validator: validateDepartureDate, trigger: 'change' }],
      returnDate: [{ validator: validateReturnDate, trigger: 'change' }],
    };
    const onSubmit = () => {
      (form.value as any).validate((valid: boolean) => {
        if (valid) {
          // alert('submit!');
          router.push('booking');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    };
    return {
      formData,
      form,
      rules,
      name1,
      name2,
      cityListArr,
      exchangeCity,
      onSubmit,
    };
  },
});
</script>

<style lang="scss" scoped>
@import '@/assets/css/config.scss';
$height: 70px;
#way-picker {
  margin-left: 14px;
  margin-top: 5px;
  height: 60px;
  line-height: 60px;
  margin-bottom: 22px;
}
//行间距
.el-row {
  margin-top: 12px;
}
//中置按钮
.form-btn-switch {
  background: url('~@/assets/img/switch.png') center no-repeat;
}
.form-btn-plane {
  background: url('~@/assets/img/plane.png') center no-repeat;
  cursor: default;
}
.form-btn-switch,
.form-btn-plane {
  width: 40px;
  margin-left: 10px;
  padding: 0;
  float: left;
  border: none;
  background-size: 40px 40px;
}
//提交按钮
.submit {
  background-color: $themeRed;
  color: white;
  width: 300px;
  margin-left: -20px;
  font-size: 20px;
  margin-top: -4px;
}
:deep(.el-input__inner) {
  width: 250px;
  margin: 0;
}
:deep(.el-input__icon) {
  width: 25px;
  text-align: center;
  -webkit-transition: all 0.3s;
  transition: all 0.3s;
  margin-right: -30px;
  line-height: 44px;
}
:deep(.deco) {
  margin-top: 7px;
  margin-right: -5px;
  width: 40px;
  float: right;
}
</style>
